package vn.axonactive.devday.jsf.handler;

import javax.faces.application.ViewHandler;
import javax.faces.application.ViewHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

public class CustomViewHandler extends ViewHandlerWrapper {

	private ViewHandler wrapped;

	public CustomViewHandler(ViewHandler wrapped) {
		this.wrapped = wrapped;
	}

	@Override
	public ViewHandler getWrapped() {
		return this.wrapped;
	}

	@Override
	public String calculateRenderKitId(FacesContext context) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		String userAgent = request.getHeader("User-Agent");

		if (userAgent.contains("Mobile")) {
			return "PRIMEFACES_MOBILE";

		} else {
			return this.wrapped.calculateRenderKitId(context);
		}
	}
}